package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.utils.UploadUtil;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {

    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")StrategyQueryObject qo, Model model){
        model.addAttribute("pageInfo",strategyDetailService.query(qo));
        model.addAttribute("strategies",strategyService.listAll());
        return "/strategyDetail/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail, MultipartFile multipartFile){
        JSONResult result = new JSONResult();
        try{
            if(multipartFile!=null&&multipartFile.getSize()>0){
                String imagePath = UploadUtil.upload(multipartFile, UploadUtil.IMG_BASE_PATH + "/upload");
                strategyDetail.setCoverUrl(imagePath);
            }
            strategyDetailService.saveOrUpdate(strategyDetail);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/getStrategyCatalogByStrategyId")
    @ResponseBody
    public Object getStrategyCatalogByStrategyId(Long strategyId){

        return strategyCatalogService.getStrategyCatalogByStrategyId(strategyId);
    }


    @RequestMapping("/getConteneByDetailId")
    @ResponseBody
    public Object getConteneByDetailId(Long detailId){

        return strategyDetailService.getStrategyContent(detailId);
    }



}
